<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogType === 'add' ? '新增用户' : '编辑用户'"
    width="800px"
    @close="closeDialog">
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      class="demo-form"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="登录账号" prop="username" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>登录账号</span>
            </template>
            <el-input v-model="form.username" placeholder="请输入登录账号" />
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <el-form-item label="姓名" prop="realName" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>姓名</span>
            </template>
            <el-input v-model="form.realName" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="角色类型" prop="roleType" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>角色类型</span>
            </template>
            <el-select 
              v-model="form.roleType" 
              placeholder="请选择角色类型" 
              style="width: 100%"
              @change="handleRoleTypeChange"
            >
              <el-option
                v-for="item in roleTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <el-form-item label="角色名称" prop="roleName" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>角色名称</span>
            </template>
            <el-input v-model="form.roleName" placeholder="请输入角色名称" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="出生日期" prop="birthDate" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>出生日期</span>
            </template>
            <el-date-picker
              v-model="form.birthDate"
              type="datetime"
              placeholder="选择日期时间"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <el-form-item label="性别" prop="gender" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>性别</span>
            </template>
            <el-radio-group v-model="form.gender">
              <el-radio v-for="item in genderOptions" :key="item.value" :label="item.value">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="状态" prop="status" class="form-item-label">
            <template #label>
              <span class="required-mark">*</span>
              <span>状态</span>
            </template>
            <el-radio-group v-model="form.status">
              <el-radio v-for="item in statusOptions" :key="item.value" :label="item.value">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <el-form-item label="组织机构" prop="organization">
            <el-select v-model="form.organization" placeholder="请选择组织机构" style="width: 100%">
              <el-option
                v-for="item in organizationOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    
    <template #footer>
      <div class="footer-buttons">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserDialog',
  props: {
    dialogVisible: Boolean,
    dialogType: String,
    formData: Object
  },
  emits: ['close', 'submit'],
  data() {
    return {
      form: {
        username: '',
        realName: '',
        roleType: '',
        roleName: '',
        birthDate: '',
        gender: '',
        status: '',
        phone: '',
        email: '',
        organization: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入登录账号', trigger: ['blur', 'change'] },
          {
            pattern: /^[A-Za-z0-9`~!@#$%^&*()_\-+={}[\]|\\:;"'<>,.?/]*$/,
            message: '只能输入数字、英文和特殊字符，不能有空格',
            trigger: ['blur', 'change'],
          },
        ],
        realName: [{ required: true, message: '请输入姓名', trigger: ['blur', 'change'] }],
        roleType: [{ required: true, message: '请选择角色类型', trigger: ['blur', 'change'] }],
        roleName: [{ required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }],
        birthDate: [{ required: true, message: '请选择出生日期', trigger: ['blur', 'change'] }],
        gender: [{ required: true, message: '请选择性别', trigger: ['blur', 'change'] }],
        status: [{ required: true, message: '请选择状态', trigger: ['blur', 'change'] }],
        phone: [
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号码',
            trigger: ['blur', 'change'],
          },
        ],
        email: [
          {
            pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change'],
          },
        ]
      },
      roleTypeOptions: [
        { label: '系统用户', value: '系统用户' },
        { label: '非系统用户', value: '非系统用户' }
      ],
      genderOptions: [
        { label: '男', value: '男' },
        { label: '女', value: '女' }
      ],
      statusOptions: [
        { label: '正常', value: '正常' },
        { label: '禁用', value: '禁用' }
      ],
      organizationOptions: [
        { label: '大学', value: '大学' },
        { label: '中学', value: '中学' 